var trap = function (height) {
  // 找到一个规律，数组经过先减后增可以收集水柱
  let left = 0
  let right = 0
  let area = 0
  let left1 = 0
  let right1 = height.length-1
  while(left1<=right1) {
    right1 = height.length - 1
    if(height[right1]>=height[right1-1]&&height[left1]>=height[left1+1]) {
      break
    }
    if(height[left1]<height[left1+1]){
      height.shift()
    }
    if(height[right1]<height[right1-1]) {
      height.pop()
    }
  }
  while (left < height.length-1) {
      left = right 
      right = left + 1
      while (right < height.length && height[right] < height[left]) {
        if(right==height.length-1) {
          break
        }
        right++
      }
      if(right-left>=2) {
          let m = Math.min(height[left],height[right])
          for(var i = left;i<right;i++) {
              area += (m-height[i])>0?(m-height[i]):0
          }
      }
  }
  return area
};
let height = [0,0,1,1,0,0]
console.log(trap(height));